<?php
	class Quanlythongbao extends CI_Controller
    {
        public function __construct() {
            parent::__construct();
            $this->load->helper(array('url', 'text')); 
            $this->load->library('checkuser');
            $this->checkuser->CheckUserLogin();             
        }
        
        public function index()
        {
            $this->LoadMainView();
        }
        
        private function LoadMainView()
        {
            $this->load->model('admin/managethongbao', 'thongbao');
            $temp['title'] = 'Thông báo';
            $temp['data']['info'] = 'Thông báo';
            $temp['template'] = 'admin/quanlythongbao';
            $content = $this->thongbao->LoadThongBao();
            $temp['data']['content'] = $content->result_array();
            $this->load->view('admin/template/layout', $temp);
        }
        
        public function VietThongBaoMoiView()
        {
            $temp['title'] = 'Thông báo';
            $temp['data']['info'] = 'Thông báo';
            $temp['template'] = 'admin/vietthongbao';
            $this->load->view('admin/template/layout', $temp);
        }
        
        public function VietThongBaoMoi()
        {
            $tieude = $this->input->post('tieude');
            $noidung = $this->input->post('editor');
            if($tieude != '')
            {                
                $this->load->model('admin/managethongbao', 'thongbao');
                $this->thongbao->TenThongBao = $tieude;
                $this->thongbao->NoiDungThongBao = $noidung;
                $this->thongbao->NgayViet = date('Y-m-d h:m:s');
                $this->thongbao->HienThi = '1';
                $result = $this->thongbao->ThemThongBaoMoi();
                if($result) $this->LoadMainView();
            }            
        }   
        
        public function XemThongBao()
        {
            $id = $this->input->get('id');
            if($id != '')
            {
                $this->load->model('admin/managethongbao', 'thongbao');                
                $temp['title'] = 'Xem thông báo';
                $temp['data']['info'] = 'Xem thông báo';
                $temp['template'] = 'admin/xemthongbao';
                $where = array('id' => $id);
                $result = $this->thongbao->LoadThongBao($where);
                if(!empty($result))  $temp['data']['baiviet'] = $result->result_array();
                $this->load->view('admin/template/layout', $temp);
            }
        }  
        
        public function CapNhatThongBao()
        {
            $id = $this->input->post('id');
            if($id != '')
            {
                $this->load->model('admin/managethongbao', 'thongbao');
                $noidung = $this->input->post('noidung');
                $tieude = $this->input->post('tieude');
                $data = array('tenthongbao' => $tieude, 'noidungthongbao' => $noidung);
                $where = array('id' => $id);
                $result = $this->thongbao->CapNhatThongBao($data, $where);
                if($result) echo 'true'; else echo 'false';
            } else echo 'false';
        }   
        
        public function XoaThongBao()
        {
            $id = $this->input->get('id');
            if($id != '')
            {
                $this->load->model('admin/managethongbao', 'thongbao');
                $where = array('id' => $id);
                $result = $this->thongbao->XoaThongBao($where);
                if($result) $this->LoadMainView();               
            }
        }
    }
?>